/****************************************************************************
**
** Copyright (C) 2010 Tieto Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Tieto Corporation (info.ostrava@tieto.com)
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Tieto.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Tieto gives you certain additional
** rights.  These rights are described in the Tieto Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Tieto at info.ostrava@tieto.com.
** $QT_END_LICENSE$
**
****************************************************************************/

#include "etalondelegate.h"

#include <QtGui> // TODO
#include "image.h"

#define ETALON_WIDTH 100;
#define ETALON_HEIGHT 20;

EtalonDelegate::EtalonDelegate(QObject *parent) :
    QItemDelegate(parent)
{
    
}

void EtalonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    ColorImage image = createEtalonFeaturesImage(index);
    // TODO painter->drawImage(0, 0, image);
}


double EtalonDelegate::computeMaximum(const QModelIndex &index) const
{
    const QAbstractItemModel *model = index.model();
    double max = 0.0;
    for (int row = 0; row < model->rowCount(); row++) {
        QModelIndex modelIndex = model->index(row, index.column());
        QVariant variant = model->data(modelIndex);
        if (variant.isValid()) {
            double value = variant.toDouble();
            max += value;
        }
    }
    return max;
}

ColorImage EtalonDelegate::createEtalonFeaturesImage(const QModelIndex &index) const
{
    const QAbstractItemModel *model = index.model();
    QVariant variant = model->data(index);
    Features features = qVariantValue<Features>(variant);
    int etalonWidth = ETALON_WIDTH;
    int etalonHeight = ETALON_HEIGHT;
    ColorImage image = ColorImage(etalonWidth, etalonHeight);
    QPainter painter(&image);
    QPen pen(Qt::SolidLine);
    pen.setWidth(2);
    QRect etalonRect(0, 0, etalonWidth, etalonHeight);
    painter.fillRect(etalonRect, Qt::green);
    for (int featureIndex = 0; featureIndex < features.size(); featureIndex++) {
        Feature feature = features[featureIndex];
        double max = computeMaximum(index);
        int featureX = 0;
        if (max) {
            featureX = feature.value / max * etalonWidth;
        }
        painter.drawLine(featureX, 0, featureX, etalonHeight);

    }
    return image;
}
